<h1 class="title-h1">提现记录</h1>
<div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-content-list">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">时间范围</label>
            <div class="layui-input-inline">
                <input type="text" name="date" id="date" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-list" lay-submit="" lay-filter="search" id="search">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 搜索
            </button>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-normal layuiadmin-btn-list" lay-submit="" lay-filter="withdraw">
                <i class="layui-icon layui-icon-rmb layuiadmin-button-btn"></i> 立即提现
            </button>
        </div>
    </div>
</div>

<div class="layui-card-body">
    <table class="layui-table" id="dataTable" lay-filter="dataTable">
    </table>

</div>
<script type="text/html" id="dataTableBar">    
    {{#  if(d.status=='待支付'){ }}
    <a class="layui-btn layui-btn-xs" lay-event="pay">支付</a> 
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="reject">驳回</a>   
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>   
    {{#  } }}
</script>
<script>
    layui.use(['table', 'http', 'form', 'laydate'], function () {
        var table = layui.table,
            layer = layui.layer,
            http = layui.http,
            $ = layui.$;
        var form = layui.form;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#date',
            range: '~',
            done: function (value, date, endDate) {
                setTimeout(() => {
                    $("#search").click()
                }, 100);
            }
        });

        form.on('submit(search)', function (data) {
            reloadTable(data.field)
            return false;
        });

        form.on('submit(withdraw)', function (data) {
            http.get('/withdraw/withdraw', '提现','500px'); 
            return false;
        });

        table.render({
            elem: '#dataTable',
            url: '/withdraw/index/',
            toolbar: '#dataTableToolbar',
            page: true,
            limit: 20,
            limits: [20, 50, 100, 500],
            cols: [
                [                    
                {if condition="($Think.session.admin_info.permission==10)"}
                {
                    field: 'name',
                    title: '供应商',
                },
                {/if}
                {
                    field: 'money',
                    title: '提现金额（元）',
                }, {
                    field: 'payment_no',
                    title: '交易号',
                }, {
                    field: 'ct',
                    title: '申请时间',
                }, {
                    field: 'status',
                    title: '状态',
                }, 
                {if condition="($Think.session.admin_info.permission==10)"} 
                {
                    field: 'handle_time',
                    title: '操作时间',
                }, {
                    title: '操作',
                    toolbar: '#dataTableBar',
                    minWidth: 150,
                    fixed: 'right'
                }
                {/if}
                ]
            ],
            parseData: function (res) {
                return {
                    "code": res.code,
                    "msg": res.message,
                    "count": res.data.count,
                    "data": res.data.data
                }
            }
        });

        var reloadTable = function (where, sort) {
            table.reload("dataTable", {
                initSort: sort,
                where: where,
            });
        }

        //监听行工具事件
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'pay') {
                layer.confirm('确定要支付吗?', {
                    skin: 'layui-layer-admin',
                }, function (index) { 
                    http.post('/withdraw/pay', {
                        id: data.id
                    }, function (res) { 
                        layer.close(index)
                        reloadTable()
                    })
                });
            } else if (obj.event === 'reject') {
                layer.confirm('确定要驳回吗?', {
                    skin: 'layui-layer-admin',
                }, function (index) { 
                    http.post('/withdraw/reject', {
                        id: data.id
                    }, function (res) { 
                        layer.close(index)
                        reloadTable()
                    })
                });
            } else if (obj.event === 'del') {
                layer.confirm('真的删除这条数据吗?', {
                    skin: 'layui-layer-admin',
                }, function (index) {
                    console.log(index)
                    http.post('/withdraw/del', {
                        id: data.id
                    }, function (res) {
                        obj.del();
                        layer.close(index);
                    })
                });
            } else if (obj.event === 'edit') {
                editFunc(data.id)
            }
        });

        table.on('sort(dataTable)', function (obj) {
            reloadTable({
                field: obj.field,
                order: obj.type
            }, obj)
        });
    });
</script>